﻿using System;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Mal.Balades.Admin.ViewModels.VisitViewModels.Edit;
using Microsoft.Maps.MapControl;

namespace Mal.Balades.Admin.Views.VisitViews.Edit
{
    public partial class MapTab : UserControl
    {
        #region Fields
        private MapLayer layer;
        private Location mouselocation;
        private TabViewModelBase viewModel;
        #endregion

        #region Construction / Destruction
        public MapTab()
        {
            InitializeComponent();

            layer = new MapLayer();
            this.map.Children.Add(layer);
            this.Loaded += new RoutedEventHandler(MapTab_Loaded);
            this.map.MouseMove += new MouseEventHandler(map_MouseMove);
            this.map.MouseDoubleClick += new EventHandler<MapMouseEventArgs>(map_MouseDoubleClick);
        }
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties

        #endregion

        #region Methods
        internal void SetLocation(double latitude, double longitude)
        {
            Location location = new Location() { Latitude = latitude, Longitude = longitude };
            //Center the image around the location specified
            PositionOrigin position = PositionOrigin.Center;

            layer.Children.Clear();

            layer.AddChild(new Pushpin(), location);

            //Add the image to the defined map layer
            map.SetView(location, map.ZoomLevel);
        }
        #endregion

        #region Events
        void MapTab_Loaded(object sender, RoutedEventArgs e)
        {
            viewModel = (TabViewModelBase)this.DataContext;

            if (viewModel.SourceVisit.Latitude.HasValue && viewModel.SourceVisit.Longitude.HasValue)
            {
                SetLocation(viewModel.SourceVisit.Latitude.Value, viewModel.SourceVisit.Longitude.Value);
            }
        }


        void map_MouseDoubleClick(object sender, MapMouseEventArgs e)
        {
            try
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us");


                viewModel.SourceVisit.Latitude = mouselocation.Latitude;
                viewModel.SourceVisit.Longitude = mouselocation.Longitude;

                //HtmlPage.Window.Invoke("SetVisitLocation"
                //    , string.Format("{0:f6}", mouselocation.Latitude)
                //   , string.Format("{0:f6}", mouselocation.Longitude));

                SetLocation(mouselocation.Latitude, mouselocation.Longitude);
            }
            catch //(Exception exp)
            {

            }
        }

        void map_MouseMove(object sender, MouseEventArgs e)
        {
            Point viewportPoint = e.GetPosition(map);

            if (map.TryViewportPointToLocation(viewportPoint, out mouselocation))
            {
                this.gps.Text = String.Format("Lat: {0:f6}, Lon: {1:f6}"
                    , mouselocation.Latitude, mouselocation.Longitude);
            }
        }
        #endregion


    }
}
